package com.qfedu.lc.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.param.UserVipAdd;
import com.qfedu.lc.user.entity.Vip;
import com.qfedu.lc.user.entity.VipOrder;
import com.qfedu.lc.user.mapper.VipMapper;
import com.qfedu.lc.user.mapper.VipOrderMapper;
import com.qfedu.lc.user.service.VipOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 9.用户vip订单表 服务实现类
 * </p>
 *
 * @author wgz
 * @since 2024-06-11
 */
@Service
public class VipOrderServiceImpl extends ServiceImpl<VipOrderMapper, VipOrder> implements VipOrderService {

    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private VipOrderMapper vipOrderMapper;

    @Override
    public VipOrder createVipOrder(UserVipAdd userVipAdd) {

        VipOrder vipOrder = new VipOrder();

        QueryWrapper<Vip> vipQueryWrapper = new QueryWrapper<>();

        vipQueryWrapper.eq("user_id",userVipAdd.getUserId());

         Vip vip = vipMapper.selectOne(vipQueryWrapper);

         if (vip == null){
             vipOrder.setType(1);// 需要查看当前用户是否已经开通vip
         }else {
             vipOrder.setType(2);
         }


        vipOrder.setCtime(new Date());
        vipOrder.setStatus(1);// 未支付
        vipOrder.setUserId(userVipAdd.getUserId());

        vipOrder.setMoney(userVipAdd.getMoney());
        vipOrder.setPayType(userVipAdd.getPayType());
        vipOrder.setVipType(userVipAdd.getVipType());

        String no = "v" + System.currentTimeMillis()+ RandomUtil.randomNumbers(6);
        vipOrder.setNo(no);

        int num = vipOrderMapper.insert(vipOrder);

        if (num>0){
            return vipOrder;
        }

        return vipOrder;
    }
}
